package com.ang.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.ang.base.Constants;
import com.ang.domain.Product;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/update")
public class UpdateController {
    /**
     * https://blog.51cto.com/u_15812686/5738680
     */

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    /**
     * 为修改造一个有id的数据
     */
    @RequestMapping("/insert")
    public void insert() throws IOException {
        Product p = new Product("id1001", "华为P30", "p30", "手机", 100000, 5999.0, DateUtil.date());

        String index = Constants.ES_INDEX_PRODUCT;

        IndexRequest indexRequest = new IndexRequest(index);
        indexRequest.source(JSON.toJSONString(p), XContentType.JSON);
        //设置操作类型，false时，_id相同会覆盖，true会报错
        indexRequest.create(false);
        //设置 _id
        indexRequest.id("_id1001");

        restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    }

    /**
     * 单个修改，修改要Map类型
     *
     */
    @RequestMapping("/updateSingle")
    public void updateSingle() throws IOException {
        Map<String, Object> newDocMap = new HashMap<>();
        newDocMap.put("create_time", DateUtil.date());

        String index = Constants.ES_INDEX_PRODUCT;
        //修改index中，_id为1001
        UpdateRequest updateRequest = new UpdateRequest(index, "_id1001");
        updateRequest.doc(newDocMap);
        updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);

        restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
    }

    /**
     * 带条件的更新:UpdateByQueryRequest
     */
    @RequestMapping("/updateWithCondition")
    public void updateWithCondition() throws IOException {
        UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(Constants.ES_INDEX_PRODUCT);
        Map<String, Object> newMap = new HashMap<>();
        newMap.put("createTime", DateUtil.date());
        //需要更新的数据
        Script script = new Script(ScriptType.INLINE, "painless", "ctx._source.create_time=newMap.createTime", newMap);
        //需要更新的条件
//        TermQueryBuilder termQueryBuilder = new TermQueryBuilder("id","id1001");
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("id","id1001");
        updateByQueryRequest.setQuery(termQueryBuilder);
        updateByQueryRequest.setScript(script);

        restHighLevelClient.updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT);
    }

    /**
     * 批量更新
     */

}
